//
//  VVYCityViewCell.m
//  viviyoo
//
//  Created by viviyoo on 15/11/10.
//  Copyright © 2015年 viviyoo. All rights reserved.
//

#import "VVYCityViewCell.h"
#import "VVYCityViewCellModel.h"

NSString *const CityViewCellReuseIdentifer = @"CityViewCellReuseIdentifer";

@interface VVYCityViewCell ()

//cell宽度
@property (nonatomic, assign) CGFloat cityModeCellW;

@end

@implementation VVYCityViewCell

+ (instancetype)cellWithTableView:(UITableView *)tableView{

    VVYCityViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CityViewCellReuseIdentifer];
    if (cell == nil) {
        cell = [[VVYCityViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CityViewCellReuseIdentifer];
    }
    return cell;
}



#pragma mark - 懒加载


#pragma mark - 重写方法
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
  
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;
      [super setSelected:selected animated:animated];
}

- (void)awakeFromNib {
    // Initialization code
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.backgroundColor = [UIColor whiteColor];
        self.separatorInset = UIEdgeInsetsMake(0, CGFLOAT_MAX, 0, 0);
        self.clipsToBounds = YES;
        self.contentView.backgroundColor = [UIColor clearColor];
    }
    return self;
}

/**
 *  重写模型set方法进行界面布局
 */
- (void)setModel:(VVYCityViewCellModel *)model{

    _model = model;
    
    [self setCitys:model.citys];
}

/**
 *  通过城市个数布局城市按钮
 */
- (void)setCitys:(NSArray *)citys{
    
    for (UIButton *btn in self.contentView.subviews) {
        if ([btn isKindOfClass:[UIButton class]]) {
            [btn removeFromSuperview];
        }
    }
    
    //根据模型个数构造按钮
    for (NSString *city in citys) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.titleLabel.font = [UIFont systemFontOfSize:14];
        [btn setTitleColor:[UIColor colorWithHexString:CityViewTextColor alpha:1] forState:UIControlStateNormal];
        [btn setTitle:city forState:UIControlStateNormal];
        [btn setBackgroundImage:[UIImage imageNamed:@"框"] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(cityBtnOnclick:) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:btn];
        
    }
}

/**
 *  重新布局
 */
- (void)layoutSubviews{
    
    NSUInteger index = 0; //索引
    NSUInteger row = 0;   //行
    NSUInteger colum = 0; //列
    CGFloat X;
    CGFloat Y;
    CGFloat W = (LYScreenWidth - (CityViewCityBtnCount + 1) * CityViewBtnMargin) / CityViewCityBtnCount;
    CGFloat H = CityViewBtnHeight;
    //城市按钮
    for (UIButton *btn in self.contentView.subviews) {
        if ([btn isKindOfClass:[UIButton class]]) {
            //设置对应btn Frame
            row = index / CityViewCityBtnCount;
            colum = index % CityViewCityBtnCount;
            X = CityViewBtnMargin + colum * (W + CityViewBtnMargin);
            Y =  row * (H + CityViewBtnMargin) + CityViewBtnMargin;
            btn.frame = CGRectMake(X, Y, W, H);
            //索引+1
            index++;
        }
    }
}



#pragma mark - 其他

/**
 *  城市选择跳转
 */
- (void)cityBtnOnclick:(UIButton *)btn{
    
    if ([self.delegate respondsToSelector:@selector(cityCell:didSelectedCity:)]) {

        NSUInteger index = [self.contentView.subviews indexOfObject:btn];

        [self.delegate cityCell:self didSelectedCity:self.model.citys[index]];
        
    }
}





@end
